<template>
  <div>
    <el-table
      :data="goodsList"
      style="width: 100%"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="商品编号"> </el-table-column>
      <el-table-column prop="goodsname" label="商品名称"> </el-table-column>
      <el-table-column prop="price" label="商品价格"> </el-table-column>
      <el-table-column prop="market_price" label="市场价格"> </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <div>
            <img :src="$pre + scope.row.img" alt="" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="scope">
          <div>
            <el-button type="success" v-if="scope.row.isnew == 1">是</el-button>
            <el-button type="danger" v-else>否</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖">
        <template slot-scope="scope">
          <div>
            <el-button type="success" v-if="scope.row.ishot == 1">是</el-button>
            <el-button type="danger" v-else>否</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            <el-button type="success" v-if="scope.row.status == 1">
              启 用</el-button
            >
            <el-button type="danger" v-else>禁 用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="primary" plain @click="edit(scope.row.id)">
              编 辑</el-button
            >
            <v-del @del="del(scope.row.id)"></v-del>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="goodsTotal"
      :page-size="goodsSize"
      @current-change="change"
    >
    </el-pagination>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { successAlert, warningAlert } from "../../../util/alert";
import { goodsDel } from "../../../request/api";
export default {
  props: [],
  components: {},
  data() {
    return {};
  },
  computed: {
    ...mapGetters({
      goodsList: "goods/list",
      goodsTotal: "goods/total",
      goodsSize: "goods/size",
    }),
  },
  methods: {
    ...mapActions({
      reqGoodsList: "goods/reqChangeList",
      reqGoodsTotal: "goods/reqChangeTotal",
      reqGoodsPage: "goods/reqChangePage",
    }),
    // 封装版本删除
    del(id) {
      goodsDel({ id: id }).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.reqGoodsList();
          this.reqGoodsTotal();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    // 编辑
    edit(id) {
      this.$emit("edit", id);
    },
    // 当页码发生变化
    change(num) {
      this.reqGoodsPage(num);
    },
  },
  mounted() {
    this.reqGoodsPage(1);
    this.reqGoodsTotal();
  },
};
</script>
<style scoped>
img {
  width: 100px;
  height: 100px;
}
</style>